/**
 * Created by KLSD on 2017/9/26.
 */

$().ready(function () {
    //给开关加默认值
    $("#isZip").val(1);

})
/**
 * 保存添加修改
 */
function saveModel() {
    if ($('#modelName').val().trim().length == 0) {
        alert("保存失败！模特名不能为空！");
    } else if (/^[\u4E00-\u9FA5A-Za-z0-9]{1,16}$/.test($("#modelName").val())) {
        if ($('#nickName').val().trim().length == 0) {
            alert("保存失败！昵称不能为空！");
        } else if (/^[\u4E00-\u9FA5A-Za-z0-9]{1,16}$/.test($("#nickName").val())) {
            var data = {
                'modelId': $('#modelId').val(),
                'modelName': $('#modelName').val(),
                'nickName': $('#nickName').val(),
                'modelIcon': $('#uploadIcon').val(),
                'modelCover': $('#uploadCover').val(),
                'modelJob': $('#modelJob').val(),
                'sex': $('#sex').val(),
                'weight': $('#weight').val(),
                'height': $('#height').val(),
                'bust': $('#bust').val(),
                'waistline': $('#waistline').val(),
                'hipline': $('#hipline').val(),
                'country': $('#country').val(),
                'city': $('#city').val()
            }
            $.ajax({
                url: "/view/saveOrupdateModel",
                type: "POST",
                dataType: "json",
                data: data,
                success: function (data) {

                    if (data.data) {
                        window.location.href = "/view/modelList?pageNumber=1&pageSize=10";
                    }
                },
            });
        } else {
            alert("保存失败！昵称由1-16中文,英文，数字组成!!");
        }
    } else {
        alert("保存失败！模特名由1-16中文,英文，数字组成!!");
    }
}
//取消事件
    function cancles() {
        window.location = "/view/modelList?pageNumber=1&pageSize=10";
    }

//上传头像
    function iconUpload() {
        $.bootstrapLoading.start({loadingTips: "正在处理数据，请稍候..."});
        var formData = new FormData();
        var files = $('#fileName')[0].files;
        for (var i = 0; i < files.length; i++) {
            formData.append("fileName", $('#fileName')[0].files[i]);
        }
        formData.append("modelId", $('#modelId').val());
        formData.append("isZip", $("#isZip").val());
        $.ajax({
            url: "/view/uploadModelIcon",
            type: "POST",
            data: formData,
            //必须false才会自动加上正确的Content-Type
            contentType: false,
            //必须false才会避开jQuery对 formdata 的默认处理
            //XMLHttpRequest会对 formdata 进行正确的处理
            processData: false,
            success: function (data) {
                if (data.data !== undefined) {
                    $('.model_Icon').attr("src", data.data.modelIcon);
                    $('#uploadIcon').val(data.data.modelIconLocal);
                }
            },
            complete: function () {
                $.bootstrapLoading.end();
            }
        });
    }


//上传封面
    function coverUpload() {
        $.bootstrapLoading.start({loadingTips: "正在处理数据，请稍候..."});
        var formData = new FormData();
        var files = $('#fileName1')[0].files;
        for (var i = 0; i < files.length; i++) {
            formData.append("fileName", $('#fileName1')[0].files[i]);
        }
        formData.append("modelId", $('#modelId').val());
        formData.append("zip", $("#isZip1").val());
        $.ajax({
            url: "/view/uploadModelCover",
            type: "POST",
            data: formData,
            //必须false才会自动加上正确的Content-Type
            contentType: false,
            //必须false才会避开jQuery对 formdata 的默认处理
            //XMLHttpRequest会对 formdata 进行正确的处理
            processData: false,
            success: function (data) {
                if (data.data !== undefined) {
                    $('.cover_Icon').attr("src", data.data.modelCover);
                    $('#uploadCover').val(data.data.modelCoverLocal);
                }
            },
            complete: function () {
                $.bootstrapLoading.end();
            }
        });
    }


function checkvalue(n, obj) {
    var oTip = document.getElementById("tip" + n);
    if (obj.value == "") {
        oTip.innerHTML = obj.title;
    }
    else {
        oTip.innerHTML = "";
    }
}

//点击开关改变值
$('#isZip').on('switch-change', function (e, data) {
    if (data.value == true) {
        $("#isZip").val(1);
    } else {
        $("#isZip").val(0);
    }
});
//点击开关改变值
$('#isZip1').on('switch-change', function (e, data) {
    if (data.value == true) {
        $("#isZip1").val(1);
    } else {
        $("#isZip1").val(0);
    }
});




